﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using myBoard.Framework;
using System.Windows.Threading;
using System.Windows.Media.Animation;

namespace myBoard.App.Controls.Notification
{
    /// <summary>
    /// Interaction logic for NotificationsWindow.xaml
    /// </summary>
    public partial class NotificationsWindow : Window
    {
        private Storyboard _hideNotificationsSB;
        private Storyboard _showNotificationsSB;

        public NotificationsWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(NotificationsWindow_Loaded);
            _hideNotificationsSB = this.Resources["HideNotifications"] as Storyboard;
            _showNotificationsSB = this.Resources["ShowNotifications"] as Storyboard;
        }

        public void ShowNotifications()
        {
            this.Show();
            this.BeginStoryboard(_showNotificationsSB);

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 15);
            timer.Tick += new EventHandler(delegate(object sender2, EventArgs e2)
            {
                timer.Stop();
                UpdateHideNotificationsStoryboard();
                this.BeginStoryboard(_hideNotificationsSB);

            });
            timer.Start();
        }

        private void UpdateHideNotificationsStoryboard()
        {
            foreach (Timeline timeline in _hideNotificationsSB.Children)
            {
                if (timeline.Name == "MarginTimeline")
                {
                    ((ThicknessAnimationUsingKeyFrames)timeline).KeyFrames[1].Value = new Thickness(0, -(this.ActualHeight + 10), 0, 0);
                }
            }
        }

        void NotificationsWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.Topmost = true;
            this.Top = 10;
            this.Left = System.Windows.SystemParameters.PrimaryScreenWidth - (this.ActualWidth + 10);            
        }

        private void _title_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            myBoard.Framework.BO.Notification notification = fe.DataContext as myBoard.Framework.BO.Notification;
            if(notification.CallBackMethod!=null)
                notification.CallBackMethod(sender, e);
        }

        private void _closeBt_Click(object sender, System.EventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            myBoard.Framework.BO.Notification notification = fe.DataContext as myBoard.Framework.BO.Notification;
            List<myBoard.Framework.BO.Notification> source = _notificationsList.ItemsSource as List<myBoard.Framework.BO.Notification>;
            source.Remove(notification);
            _notificationsList.ItemsSource = source;
            if (source.Count == 0)
                this.Close();
        }
    }
}
